/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.template;

import offset.nodes.client.dialog.newnode.model.NameProvider;
import offset.nodes.client.model.Server;
import offset.nodes.client.chooser.view.QueryResultChooserDialog;
import offset.nodes.client.model.Validator;
import offset.nodes.client.view.SimpleDialog;

/**
 *
 * @author  Walter Lütgenau
 */
public class SelectTemplatePanel extends javax.swing.JPanel implements Validator, NameProvider {

    NameProvider nodeTypeNameProvider = null;
    Server model;

    /** Creates new form SelectNodeTypePanel */
    public SelectTemplatePanel(Server model, NameProvider nodeTypeNameProvider) {
        this.nodeTypeNameProvider = nodeTypeNameProvider;
        this.model = model;

        initComponents();
    }

    public boolean isValidPane() {
        if (templateTextField.getText() != null && templateTextField.getText().length() > 0) {
            return true;
        }
        return false;
    }

    public String getObjectName() {
        return templateTextField.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        templateLabel = new javax.swing.JLabel();
        templateTextField = new javax.swing.JTextField();
        templateSelectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/vdialog/view/DialogBundle"); // NOI18N
        templateLabel.setText(bundle.getString("templateFolder.step2.templateLabel")); // NOI18N

        templateTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                templateTextFieldActionPerformed(evt);
            }
        });

        templateSelectButton.setText(bundle.getString("templateFolder.step1.selectNodeTypeButton")); // NOI18N
        templateSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectTemplate(evt);
            }
        });

        jScrollPane1.setBorder(null);

        jTextPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        jTextPane1.setBorder(null);
        jTextPane1.setText(bundle.getString("templateFolder.step2.description")); // NOI18N
        jScrollPane1.setViewportView(jTextPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(templateLabel)
                        .addGap(18, 18, 18)
                        .addComponent(templateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(templateSelectButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(templateLabel)
                    .addComponent(templateSelectButton)
                    .addComponent(templateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void templateTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_templateTextFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_templateTextFieldActionPerformed

    private void selectTemplate(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectTemplate
        QueryResultChooserDialog chooser = null;
        
        try {
            String root = "//*[@jcr:primaryType='nodes:template' and @nodes:nodeType='" + nodeTypeNameProvider.getObjectName() + "']";
            chooser = new QueryResultChooserDialog(new javax.swing.JFrame(), true, model, "//element(*,nodes:template)[@nodes:nodeType='" + nodeTypeNameProvider.getObjectName() + "']");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        chooser.setVisible(true);
        if (chooser.getResult() == SimpleDialog.OK) {
            templateTextField.setText(chooser.getPath());
        }
    }//GEN-LAST:event_selectTemplate
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JLabel templateLabel;
    private javax.swing.JButton templateSelectButton;
    private javax.swing.JTextField templateTextField;
    // End of variables declaration//GEN-END:variables
}
